//
//  BodyCheckCheckResultListViewController.m
//  TryShopping
//
//  Created by panquanReabam.com on 16/9/8.
//  Copyright © 2016年 RuiBen. All rights reserved.
//

#import "BodyCheckCheckResultListViewController.h"
#import "BodyCheckCheckResultDetailViewController.h"
#import "MJRefresh.h"
#import "BodyCheckCheckResultListModel.h"
#import "BodyCheckCheckResultListCell.h"
#import "FilterView.h"
@interface BodyCheckCheckResultListViewController ()<MBProgressHUDDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITextFieldDelegate,UISearchBarDelegate,FilterViewDelegate>
@property (strong, nonatomic) NSString*sats;
@end
@implementation BodyCheckCheckResultListViewController
{
    MJRefreshHeaderView* _header;
    MJRefreshFooterView* _footer;
   int pageNumber;
    NSMutableArray*ModelArray;
    NSString* TotalCount;
    UITableView *table;
    UIButton*btnNormal;
    UIButton*screeningBtn;
    UIButton*screeningImage;
    MBProgressHUD *progressHud;
    NSMutableArray*mItemsArray;
    UIView*tableHeaderView;
    UIView*noDataView;
}
@synthesize alt = alt;
-(void)BackAction:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];

}
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    self.navigationController.delegate = nil;
    
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden=NO;
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor blackColor],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    self.title=@"检测报告";//未支付
    
}
-(void)update:(NSString *)str{
    [self addTable];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self addTable];
    [self addFooter];
    [self addheader];
}
-(void)addTable{
    [table removeFromSuperview];
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    table.separatorStyle=UITableViewCellSeparatorStyleNone;
    table.delegate=self;
    table.dataSource=self;
    [table registerClass:[BodyCheckCheckResultListCell class] forCellReuseIdentifier:@"BodyCheckCheckResultListViewController"];
    [self.view addSubview: table];

    ModelArray=[[NSMutableArray alloc]init];
    [mItemsArray removeAllObjects];
    [noDataView removeFromSuperview];
   
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:@"1" forKey:@"pageIndex"];
    [param setValue:@"20" forKey:@"pageSize"];
     [param setValue:self.memberId forKey:@"memberId"];
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    NSLog(@"====%@",param);
    [HttpRequest requestData:@"BodyCheck/CheckResult/List" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            NSLog(@"====%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                mItemsArray=[NSMutableArray arrayWithArray:[severDataDict.dataDict objectForKey:@"DataLine"]];
                TotalCount=[NSString stringWithFormat:@"%@",[severDataDict.dataDict objectForKey:@"TotalCount"]];
                if (mItemsArray.count>0) {
                    for (NSDictionary *dic in mItemsArray) {
                        BodyCheckCheckResultListModel *dataModel=[BodyCheckCheckResultListModel dataModelWithBodyCheckCheckResultListDict:dic];
                        [ModelArray addObject:dataModel];
                        
                    }
                    
                }else{
                    noDataView=[[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight/2-50, ScreenWidth, 80)];
                    [self.view addSubview:noDataView];
                    UIImageView*noImage=[[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth/2-47, 0, 94, 51)];
                    noImage.image=[UIImage imageNamed:@"meiyoushuju"];
                    [noDataView addSubview:noImage];
                    UILabel*noLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,65, ScreenWidth, 14)];
                    noLabel.text=@"没有列表数据，赶紧去添加！";
                    noLabel.textColor=zhonghuiColor; noLabel.font=[UIFont systemFontOfSize:14];
                    noLabel.textAlignment=NSTextAlignmentCenter;
                    [noDataView addSubview:noLabel];
                }
                
                [table reloadData];
            }else{
                [Global hideProgressHUDInternal:self.view];
               
                
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }
        
    }];
    
}

//下拉刷新
- (void)addheader
{
    MJRefreshHeaderView* footer = [MJRefreshHeaderView header];
    footer.scrollView = table;
    footer.beginRefreshingBlock = ^(MJRefreshBaseView* refreshView)
    {
        //        searchBeans=nil;
        //        self.sats=@"newOrder";
        //        _orderSort=@"desc";
        //        [self requestData];
        [self performSelector:@selector(doneWithView:) withObject:refreshView afterDelay:1.0f];
        
    };
}
//上拉刷新
- (void)addFooter
{
    MJRefreshFooterView* footer = [MJRefreshFooterView footer];
    footer.scrollView = table;
    footer.beginRefreshingBlock = ^(MJRefreshBaseView* refreshView)
    {
        
        if (TotalCount.doubleValue/20.00>=pageNumber) {
            pageNumber++;
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            NSString* Token = [user objectForKey:@"tokenId"];
            NSMutableDictionary *param = [NSMutableDictionary new];
            [param setValue:Token forKey:@"tokenId"];
            [param setValue:[NSString stringWithFormat:@"%d" ,pageNumber] forKey:@"pageIndex"];
            [param setValue:@"20" forKey:@"pageSize"];
            [param setValue:self.memberId forKey:@"memberId"];
            [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
            [HttpRequest requestData:@"BodyCheck/CheckResult/List" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
                if (isSuccess) {
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                        [Global hideProgressHUDInternal:self.view];
                        mItemsArray=[NSMutableArray arrayWithArray:[severDataDict.dataDict objectForKey:@"DataLine"]];
                        for (NSDictionary *dic in mItemsArray) {
                            BodyCheckCheckResultListModel *dataModel=[BodyCheckCheckResultListModel dataModelWithBodyCheckCheckResultListDict:dic];
                            [ModelArray addObject:dataModel];
                        }
                        [table reloadData];
                    }else{
                        [Global hideProgressHUDInternal:self.view];
                      }}
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    [Global showTextHudSub:severDataDict.resultStr view:self.view];
                }
                
            }];
            
            
        }else{
            
            NSString *requestResultStr=@"没有更多数据了！";
            [Global showTextHudSub:requestResultStr view:self.view];
            
        }
        
        [self performSelector:@selector(doneWithView:) withObject:refreshView afterDelay:3.0f];
        NSLog(@"%@--->正在刷新", refreshView.class);
    };
}
- (void)doneWithView:(MJRefreshBaseView *)refreshView
{
    [refreshView endRefreshing];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [ModelArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BodyCheckCheckResultListCell*cell = [tableView dequeueReusableCellWithIdentifier:@"BodyCheckCheckResultListViewController"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model=ModelArray[indexPath.row];
    return cell;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return  100;
}

#pragma mark - Table view delegate


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   BodyCheckCheckResultListModel *ScreeningModel=ModelArray[indexPath.row];
    BodyCheckCheckResultDetailViewController *detailViewController = [[BodyCheckCheckResultDetailViewController alloc] init];
    detailViewController.orderId=ScreeningModel.reportId;
    [self.navigationController pushViewController:detailViewController animated:YES];
}
- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}

@end

